﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCP.UI.Blocks;
using Baosight.Platform.Core.ServiceModel;
using System.Windows.Browser;

namespace Baosight.ZNH.ZMCP.UI
{
    /// <summary>
    /// 生产日志
    /// </summary>
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张志芳
    /// Version: 1.0
    /// History:
    ///       2011-07-08  张志芳   [创建]
    ///
    public partial class ZMCPD0 : EFViewCmes
    {
        public ZMCPD0()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Baosight.Platform.Core.EIInfo initData)
        {
            var date = initData.GetBlock(EIConstants.QueryStatusBlock).GetRow(0)["fdDate"];
            this.titleDate.Value = date;
            this.titleText.Text = DateTime.Parse(date.ToString()).ToString("yyyy年MM月dd日") + "  生 产 日 志";
            base.Initialize(initData);
        }
        
        /// <summary>
        /// 根据日期更改标题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void titleDate_ValueChanged(object sender, Platform.Core.UI.Controls.ValueChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                String strDate = e.NewValue as string;
                this.titleText.Text = DateTime.Parse(strDate).ToString("yyyy年MM月dd日") + "  生 产 日 志";

                RefreshData();
                this.OnQueryButtonClick(null, null);
            }
        }

        /// <summary>
        /// 页签切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {    
            RefreshData();
        }

        /// <summary>
        /// 刷新页签页面
        /// </summary>
        private void RefreshData()
        {
            if (logTab.SelectedItem != null)
            {                
                EFTabItem item = logTab.SelectedItem as EFTabItem;
                item.Content = null;

                if (item.Name.Equals("tab1"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdDate", this.titleDate.Value.ToString());
                    item.LoadView("ZMCPD1", args);
                }
                else if (item.Name.Equals("tab2"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdDate", this.titleDate.Value.ToString());
                    item.LoadView("ZMCPD2", args);
                }
                else if (item.Name.Equals("tab3"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("view", "ZMCPD3");
                    args.Add("fdVirtualDate", this.titleDate.Value.ToString());
                    args.Add("fdStatus", "1");
                    item.LoadView("ZMCPD3", args);
                }
                else if (item.Name.Equals("tab5"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("view", "ZMCPD5");
                    args.Add("fdDate", this.titleDate.Value.ToString());
                    item.LoadView("ZMCPD5", args);
                }
                else if (item.Name.Equals("tab9"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdDate", this.titleDate.Value.ToString());
                    item.LoadView("ZMCPDB", args);
                }
                else if (item.Name.Equals("tab10"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdDate", this.titleDate.Value.ToString());
                    item.LoadView("ZMCPDC", args);
                }
            }
        }

        /// <summary>
        /// 重新基类的方法，当有新的EIInfo 数据发布时回调的方法
        /// 生产进度新增修改后更新统计数据
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(EIInfo data)
        {
            //刷新统计数据
            UpdateStatisticsData();
        }

        /// <summary>
        /// 更新统计数据
        /// </summary>
        private void UpdateStatisticsData()
        {
            EIInfo info = new EIInfo();
            info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCPD0");
            info.Attributes.Add(EIConstants.MethodNameKey, "dataStatistics");
            info.AddBlock(this.EIDataContext.GetBlock(EIConstants.QueryStatusBlock));

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(info, OnDoWorkCompleted);
        }

        /// <summary>
        /// UpdateStatisticsData回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.EIDataContext.Update(e.EIInfo.Blocks[EIConstants.ResultBlock]);
        }

        /// <summary>
        /// 点击生成日志事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnReportButtonClick(object sender, RoutedEventArgs e)
        {
            string year = this.EIDataContext.GetAttribute("year").ToString();
            string month = this.EIDataContext.GetAttribute("month").ToString();
            string beginDate = this.EIDataContext.GetAttribute("beginDate").ToString();
            int dayNum = getDayNum(beginDate, this.titleDate.Value.ToString());
            if (dayNum < 0)
            {
                BaseTools.OpenEFWindowAlert("当月的生产统计月起始日大于当前日期，请重新设置！", null);
                return;
            }
            Uri uri = Uploader.ConstructAbsoluteUri("../admin/RunmaxReport.jsp?projectName=CMESReport&reportName=produceReport&date=" + this.titleDate.Value.ToString() + "&curYear=" + year + "&curMonth=" + month + "&beginDate=" + beginDate + "&dayNum=" + dayNum.ToString());
            HtmlPage.Window.Navigate(uri, "_blank");
        }

        private int getDayNum(string beginDay, string endDay)
        {
            DateTime begin = Convert.ToDateTime(beginDay);
            DateTime end = Convert.ToDateTime(endDay);
            return (end - begin).Days + 1;
        }
    }
}
